#! usr/bin/python


from PyQt4 import QtCore, QtGui
import sys
import matplotlib as m
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np


class Spectrogram(QtGui.QWidget):
	
	
	def __init__(self, spl):
		QtGui.QWidget.__init__(self)
		self.spl =spl
		self.make_gui()
	
	
	
	def make_gui(self):
		
		vb = QtGui.QVBoxLayout()
		
		intensity =list(); mass =list(); rt =list()
		for i in xrange(len(self.spl.getSpectra())):
			intensity.extend(self.spl.getSpectra()[i].intens)
			mass.extend(self.spl.getSpectra()[i].mass)
			for j in xrange (len(self.spl.getSpectra()[i].mass)):
				rt.append(self.spl.getSpectra()[i].t_min)
				

		x = np.array(mass)
		y = np.array(rt)
		Z = np.log(np.array(intensity))
		
		fig=Figure(figsize=(5, 4), dpi=76, facecolor='w')
		sub = fig.add_subplot(111,axisbg='w',xlabel="Retention Time",ylabel="transition")
		sub.scatter(y, x, c= Z, cmap='jet', edgecolors ='none', vmin=min(Z), vmax=max(Z))
		can=FigureCanvas(fig)
		can.setParent(self)
		mpl_toolbar = NavigationToolbar(can,self)
		vb.addWidget(mpl_toolbar)
		vb.addWidget(can)
		self.setLayout(vb)
	
	def get_canvas(self):
		return self
